package lesson02;

public class Demo_201 {
    public static void main(String[] args) throws InterruptedException {
        //并行
        concurrency();
        //串行
        serial();

    }

    public static long n = 10_0000_0000;
    public static void concurrency() throws InterruptedException {
    //并行
        long begin = System.currentTimeMillis();
        //创建线程
        Thread thread1 = new Thread(() -> {
            int a = 0;
            for (int i = 0; i < n; i++) {
                a--;
            }
        });

        Thread thread2 = new Thread(() -> {
            int b = 0;
            for (int i = 0; i < n; i++) {
                b--;
            }
        });
        //启动线程
        thread1.start();
        thread2.start();

        //等待线程完成
        thread1.join();
        thread2.join();
        long end = System.currentTimeMillis();
        System.out.println("并行消耗时间" + (end - begin));
    }
    public static void serial(){
        //串行
        long begin = System.currentTimeMillis();
        int a = 0;
        for (int i = 0; i < n; i++) {
            a--;
        }
        int b = 0;
        for (int i = 0; i < n; i++) {
            b--;
        }
        long end = System.currentTimeMillis();

        System.out.println("串行消耗时间" + (end - begin));
    }
}
